//
//  ICMSelecteItemDataView.m
//  ICrEAM
//
//  Created by rainimute on 16/11/19.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMSelecteItemDataView.h"

@interface ICMSelecteItemDataView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTableView;

@property (nonatomic, strong) NSArray *itemDatas;

@property (nonatomic, assign) float itemWidth;

@end

@implementation ICMSelecteItemDataView

- (instancetype)initSelecteItemDataViewWithSourceView:(UIView *)sourceView andItemDatas:(NSArray *)itemDatas frame:(CGRect)frame{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);

        UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width,frame.size.height)];
        blankView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView)];
        [blankView addGestureRecognizer:tap];
        self.itemDatas = itemDatas;
        
        
        CGRect sourceViewWindowFrame = [sourceView convertRect:sourceView.frame toView:self];
        
        self.itemWidth = CGRectGetWidth(sourceViewWindowFrame);
        
        self.dataTableView = [[UITableView alloc]initWithFrame:CGRectMake(sourceView.x, sourceView.y+sourceView.height, sourceView.width,itemDatas.count<3?ActualHeight(44)*itemDatas.count: ActualHeight(44)*3)];
        [self.dataTableView registerClass:[ICMSelecteItemCell class] forCellReuseIdentifier:@"ICMSelecteItemCell"];
        self.dataTableView.delegate = self;
        self.dataTableView.dataSource = self;
        CornerRadius(self.dataTableView, RadiusValue);
        BorderWidth(self.dataTableView, 0.5);
        BorderColor(self.dataTableView, BorderColor_Gray_Hex);
        [self addSubview:blankView];
        [self addSubview:self.dataTableView];
        
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICMSelecteItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ICMSelecteItemCell"];
    if (!cell) {
        cell = [[ICMSelecteItemCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ICMSelecteItemCell"];
    }
    cell.itemWidth = self.itemWidth;
    cell.itemTitle = self.itemDatas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *itemTitle = [self.itemDatas objectAtIndex:indexPath.row];
    if ([self.delegate respondsToSelector:@selector(didSelectedItemTitle:)]) {
        [self.delegate didSelectedItemTitle:itemTitle];
        [self removeFromSuperview];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(44);
}

- (void)hideView {
    [self removeFromSuperview];
}


+ (instancetype)createSelecteItemDataViewWithSourceView:(UIView *)sourceView andItemDatas:(NSArray *)itemDatas frame:(CGRect)frame{
    return [[ICMSelecteItemDataView alloc]initSelecteItemDataViewWithSourceView:sourceView andItemDatas:itemDatas frame:frame];
}

@end


@implementation ICMSelecteItemCell

- (void)setItemTitle:(NSString *)itemTitle{
    self.itemTitleLabel.text = itemTitle;
}

- (UILabel *)itemTitleLabel{
    if (!_itemTitleLabel) {
        _itemTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.itemWidth, ActualHeight(44))];
        [_itemTitleLabel setUpLabelWithText:@"" alignment:NSTextAlignmentCenter textSize:14 textColor:[UIColor blackColor]];
        [self.contentView addSubview:_itemTitleLabel];
    }
    return _itemTitleLabel;
}

@end











